/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __FRUSTRUM_H__
#define __FRUSTRUM_H__

#include "Matrix4.h"
#include "Intersection.h"
#include "HalfSpace.h"

namespace liba
{
namespace math
{
class Sphere;

// Represents a viewing frustum. Used for visibility checks.
// The frustum is positioned at the origin and looks down the positive z-Axis
// after construction. It can then be transformed for
// positioning in world space.

class Frustrum
{
	typedef math::Plane<float> Plane;
public:
	Frustrum()
	{}

	//perspective frustrum
	Frustrum(	const float & near_plane, const float & far_plane,
				const float & fov_horizontal, const float & fov_vertical );

	//orthogonal frustrum
	Frustrum(	const float & near_plane, const float & far_plane,
				const float & left_plane, const float & right_plane,
				const float & top_plane, const float & bottom_plane );

	void transform( const Matrix4<float> & matrix )
	{
		for( int i = 0; i < 6; ++i )
		{
			spaces[i].transform( matrix );
		}
	}

	Intersect::Type intersects( const Sphere & sphere )const;
	Intersect::Type intersects( const Box & box )const;

	enum HalfSpaceType
    {
        NEAR_PLANE,
        FAR_PLANE,
        LEFT_PLANE,
        RIGHT_PLANE,
        TOP_PLANE,
        BOTTOM_PLANE
    };

	const HalfSpace & get_halfspace(HalfSpaceType type)const
	{
		return spaces[type];
	}

private:
	// Order is near, far, left, right, top, bottom
	HalfSpace spaces[6];
};

} // namespace math
} // namespace liba
using namespace liba;

#endif //__FRUSTRUM_H__
